package com.itheima.canal;

import com.itheima.dao.MoneyLogDao;
import com.itheima.domain.MoneyLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

import java.util.List;

/*****
 * @Author: http://www.itheima.com
 * @Description: com.itheima.canal.MoneyLogAsync
 ****/
@Component
@CanalTable(value = "money_log")    //监听的表
//泛型：当前类要监听的表对应的Bean
public class MoneyLogAsync  implements EntryHandler<MoneyLog> {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MoneyLogDao moneyLogDao;

    @Autowired
    private RestTemplate restTemplate;


    @Override
    public void insert(MoneyLog moneyLog) {
        //更新用户的红包记录
        List<MoneyLog> list = moneyLogDao.list(moneyLog.getUsername());
        redisTemplate.boundHashOps("UserMoneyLog").put(moneyLog.getUsername(),list);

        try {
            //更新排行榜红包记录   请求地址 http://192.168.23.128/purge/mlog/top
            restTemplate.getForObject("http://192.168.23.128/purge/mlog/top",String.class);
        } catch (RestClientException e) {
        }
    }

    @Override
    public void update(MoneyLog before, MoneyLog after) {
        System.out.println("update--before"+before);
        System.out.println("update--after"+after);
    }

    @Override
    public void delete(MoneyLog moneyLog) {
        System.out.println("delete"+moneyLog);
    }
}
